package com.shitou.shitoucommons.utils;

import com.shitou.shitoucommons.exception.RRException;
import com.shitou.shitoucommons.xss.SQLFilter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.sql.*;

/**
 * class detail:
 *
 * @author 石头人
 */
@SuppressWarnings("all")
@Data
@AllArgsConstructor
@ToString
@Slf4j
public class JDBCUtils {

    private static Connection connection = null;

    private static String username = "root";
    private static String password = "root";
    private static String url = "jdbc:mysql://localhost:3306/placeholder?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";

    public JDBCUtils(String database){
        database = SQLFilter.sqlInject(database);
        this.url = this.url.replace("placeholder", database);
    }
    public JDBCUtils(String database, String username, String password){
        database = SQLFilter.sqlInject(database);
        this.url = this.url.replace("placeholder", database);
        this.username = username;
        this.password = password;
    }

    /**
     * 当自身主键没有作为其他外键的时候才可以调用此方法，
     *      否则会在其它地方发生未知错误
     * @param database 数据库
     * @param table 表名
     * @param auto_increment_beginNumber 将自增主键修改从哪一位开始
     *          如果将 auto_increment_beginNumber 设置为 1 那么会将这个表中的所有数据清除
     *          建议不要设为1
     * @param isTruncate 是否清除该表数据, 默认不开启
     * @return
     */
    public static void renewAutoIncrement(String username, String password, String database, String table, long auto_increment_beginNumber, boolean isTruncate){
        table = SQLFilter.sqlInject(table);
        database = SQLFilter.sqlInject(database);
        // 似乎这里也没必要，懒得删了
        auto_increment_beginNumber = Long.parseLong(SQLFilter.sqlInject(String.valueOf(auto_increment_beginNumber)));

        url = url.replace("placeholder", database);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            /**
             * 自增主键设为1，清除该表, 这是不安全的
             */
            if ( auto_increment_beginNumber == 1 || isTruncate){
                PreparedStatement preparedStatement = connection.prepareStatement("truncate " + table);
                preparedStatement.execute();
                System.out.println("进行了清除表数据");
            }
            String sql = "ALTER TABLE " + table + " AUTO_INCREMENT = " + auto_increment_beginNumber + ";";
            System.out.println(sql);
            // 获得处理对象
            Statement statement = connection.createStatement();
            // 执行更新
            statement.execute(sql);

        } catch (SQLException e) {
            log.error("恢复自增主键发生错误: {}", e.getMessage());
            throw new RRException("恢复自增主键发生错误");
        } catch (ClassNotFoundException e) {
            log.error("在加载驱动时com.mysql.jdbc.Driver时，驱动类未找到: {}", e.getMessage());
            throw new RRException("在加载驱动时com.mysql.jdbc.Driver时，驱动类未找到");
        }
    }
    public static void renewAutoIncrement(String database, String table, long auto_increment_beginNumber){
        renewAutoIncrement(username, password, database, table, auto_increment_beginNumber, false);
    }
    public static void renewAutoIncrement(String username, String password, String database, String table, long auto_increment_beginNumber){
        renewAutoIncrement(username, password, database, table, auto_increment_beginNumber, false);
    }
    public static void renewAutoIncrement(String database, String table, long auto_increment_beginNumber, boolean isTruncate){
        renewAutoIncrement(username, password, database, table, auto_increment_beginNumber, isTruncate);
    }

    public static void main(String[] args) {
        renewAutoIncrement("test", "member", 1, true);
    }
}
